package com.one.piece.controller;


import com.one.piece.document.Customer;
import com.one.piece.service.CustomerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping(value = "customer/")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    private static final Logger LOG = LoggerFactory.getLogger(CustomerController.class);


    @RequestMapping(value = "/hello")
    public String hello() {
        return "hello";
    }

    @RequestMapping(value = "findCustomer", method = RequestMethod.POST)
    public Object findCustomer(String id) {
        return customerService.findCustomer(id);
    }

    @RequestMapping(value = "save")
    public Object save(Customer customer) {
        return customerService.save(customer);
    }

    /**
     * 删除id不存在的索引，也不会有任何异常。
     */
    @RequestMapping(value = "delete")
    public String delete(String id){
        customerService.delete(id);
        return "succeed";
    }

    @RequestMapping(value = "findAll")
    public Page<Customer> findAll(@PageableDefault Pageable pageable) {
        LOG.info("/rest/book1 -> pageSize = {}, pageNumber = {}",
                pageable.getPageNumber(), pageable.getPageSize());
        return customerService.findAll(pageable);
    }
    @RequestMapping(value = "findAllSort")
    public Page<Customer> findAllSort(@PageableDefault Pageable pageable) {
        LOG.info("/rest/book1 -> pageSize = {}, pageNumber = {}",
                pageable.getPageNumber(), pageable.getPageSize());
        return customerService.findAllSort(pageable);
    }


    @RequestMapping(value = "findByFirstNameOrLastName")
    public List<Customer> findByFirstNameOrLastName(String firstName, String lastName) {
        return customerService.findByFirstNameOrLastName(firstName, lastName);
    }


    @RequestMapping(value = "findByFirstNameAndLastName")
    List<Customer> findByFirstNameAndLastName(String firstName, String lastName) {
        return customerService.findByFirstNameAndLastName(firstName, lastName);
    }

    @RequestMapping(value = "findByFirstNamePage")
    Page<Customer> findByFirstName(String firstName, @PageableDefault Pageable pageable) {
        return customerService.findByFirstName(firstName, pageable);
    }

    @RequestMapping(value = "findByLastNamePage")
    Page<Customer> findByLastName(String lastName, @PageableDefault Pageable pageable) {
        return customerService.findByLastName(lastName, pageable);
    }

    @RequestMapping(value = "findByFirstNameOrLastNamePage")
    Page<Customer> findByFirstNameOrLastName(String firstName, String lastName, @PageableDefault Pageable pageable) {
        return customerService.findByFirstNameOrLastName(firstName, lastName, pageable);
    }

    @RequestMapping(value = "findByFirstNameAndLastNamePage")
    Page<Customer> findByFirstNameAndLastName(String firstName, String lastName, @PageableDefault Pageable pageable) {
        return customerService.findByFirstNameAndLastName(firstName, lastName, pageable);
    }

    @RequestMapping(value = "findByAgeBetweenPage")
    Page<Customer> findByAgeBetween(int from, int to, @PageableDefault Pageable pageable) {
        return customerService.findByAgeBetween(from, to, pageable);
    }

    @RequestMapping(value = "findByFirstNameLikePage")
    Page<Customer> findByFirstNameLike(String firstName, @PageableDefault Pageable pageable) {
        return customerService.findByFirstNameLike(firstName, pageable);
    }

    @RequestMapping(value = "findByAddress")
    Page<Customer> findByAddress(String address, @PageableDefault Pageable pageable) {
        return customerService.findByAddress(address, pageable);
    }

    @RequestMapping(value = "search")
    Page<Customer> search(String id, @PageableDefault Pageable pageable) {
        return customerService.search(id, pageable);
    }


}